<template>
	<div class="my">
		<view>
			<picker  @click="inputClick('project')" @change="bindPickerChange" :value="index" :range="project">
				<input class="inputStyle" placeholder="请选择报修项目" disabled v-model="stu.project" />
				<view class="imgRight"></view>
			</picker>
		</view>
		<!-- <view>
			<picker  @click="inputClick('campus')" @change="bindPickerChange" :value="index" :range="campus">
				<input class="inputStyle" placeholder="请选择校区" disabled v-model="stu.campus" />
				<view class="imgRight"></view>
			</picker>
		</view>
		<view>
			<picker  @click="inputClick('building')" @change="bindPickerChange" :value="index" :range="building">
				<input class="inputStyle" placeholder="请选择楼栋" disabled v-model="stu.building" />
				<view class="imgRight"></view>
			</picker>
		</view> -->
		<view>
			<picker  @click="inputClick('houseNumber')" @change="bindPickerChange" :value="index" :range="houseNumber">
				<input class="inputStyle" placeholder="请选择宿舍号" disabled v-model="stu.houseNumber" />
				<view class="imgRight"></view>
			</picker>
		</view>
		<view>
			<input class="inputStyle" placeholder="请填写联系方式" v-model="stu.contact" />
		</view>
		<view>
			<picker mode="date" :value="stu.startDate" :start="startDate" :end="endDate" @change="bindDateChange">
				<input class="inputStyle" placeholder="请选择维修时间" disabled/>
				<view class="imgRight"></view>
				<view class="myDate">{{stu.startDate}}</view>
			</picker>
		</view>
		<view>
			<textarea class="inputStyle" placeholder="请填写维修事宜" v-model="stu.matters"/>
		</view>
		<view>
			<button type="default" form-type="submit" class="loginBtn" @click="login">提交</button>
		</view>
	</div>
</template>

<script>
	const {repair} = require('../../utils/api.js')
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				percent:0,
				project: ['桌子', '椅子', '衣柜', '水龙头'],
				campus: ['前湖校区', '青山湖校区', '东湖校区'],
				building: ['1栋', '2栋', '3栋','4栋', '5栋', '6栋'],
				houseNumber: ['210', '211','212', '213'],
				index: 0,
				stu:{
					project:'',
					campus:'',
					building:'',
					houseNumber:'',
					contact:'',
					startDate: currentDate,
					matters:''
				},
				id:''
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		methods:{
			bindDateChange: function(e) {
				this.stu.startDate = e.target.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			inputClick(id){
				this.id = id;
			},
			bindPickerChange: function(e) {
				this.index = e.target.value;
				if(this.id == 'project'){
					this.stu.project = this.project[this.index];
				}else if(this.id == 'campus'){
					this.stu.campus = this.campus[this.index];
				}else if(this.id == 'building'){
					this.stu.building = this.building[this.index];
				}else if(this.id == 'houseNumber'){
					this.stu.houseNumber = this.houseNumber[this.index];
				}
			},
			login(){
				repair(this.stu).then(data=>{
					uni.showModal({
						content: data.msg,
						showCancel: false
					});
					if(data.code === 200){
						uni.navigateTo({
							url: '../pay/success'
						});
					}
				})
			}
		}
	}
</script>

<style scoped src="../../static/css/my.css"></style>
<style scoped src="../../static/css/service.css"></style>
